<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form class="form" :model="form" :rules="rules" ref="form">
          <div class="left">
            <el-form-item label="关键字" label-width="80px" >
            <el-input style="max-width:230px" placeholder="根据文章标题搜索" v-model="form.keyword"></el-input>
          </el-form-item>
          <el-form-item label="状态" label-width="80px">
              <el-select v-model="form.value" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
          </el-form-item>
          <el-form-item class="btn" label-width="15px">
            <el-button @click="clearFn">清除</el-button>
          </el-form-item>
           <el-form-item label-width="15px">
            <el-button @click="searchFn" type="primary">搜索</el-button>
          </el-form-item>
          </div>
          <div class="right">
             <el-form-item class="suc">
            <el-button type="success" icon="el-icon-edit" @click="openDrawer('add')">新增技巧</el-button>
          </el-form-item>
          </div>
        </el-form>
         <div class="header">
            <span class="el-icon-info">数据一共{{totals}}条</span>
          </div>
          <el-table :data="tableData" style="width:100%">
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column label="文章标题" prop="title">
              <template #default='{row}'>
               {{row.title}}
               <el-button type="text" @click="videoFn(row)" v-if="row.videoURL !==''">
                <i class="el-icon-video-camera-solid"></i>
               </el-button>
              </template>
            </el-table-column>
            <el-table-column label="阅读数" prop="visits"></el-table-column>
            <el-table-column label="录入人" prop="username"></el-table-column>
            <el-table-column label="录入时间" prop="createTime">
              <template #default='date'>
                {{date.row.createTime|parseTimeByString}}
              </template>
            </el-table-column>
            <el-table-column label="状态" prop="state">
              <template slot-scope="scope">
               {{ scope.row.state === 1 ? "已启用" : "已禁用" }}
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                 <div class="actions">
                  <el-link type="primary" @click="openDrawer('preview',scope.row.id)"  :underline="false">预览</el-link>
                  <el-link  type="primary" :underline="false" @click="stateFn(scope.row)"><span v-if="scope.row.state===1">禁用</span><span v-else>启用</span></el-link>
                  <el-link v-if="scope.row.state===0" type="primary"   :disabled='scope.row.state===1' :underline="false" @click="openDrawer('edit',scope.row.id)">修改</el-link>
                  <el-link type="info" v-else :disabled='scope.row.state===1' :underline="false" @click="openDrawer('edit',scope.row.id)">修改</el-link>
                  <el-link  @click="del(scope.row.id)" v-if="scope.row.state===0" type="primary" :disabled='scope.row.state==1' :underline="false">删除</el-link>
                  <el-link  @click="del(scope.row.id)" v-else type="info" :disabled='scope.row.state==1' :underline="false">删除</el-link>
              </div>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination background style="float:right" class="pag"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout=" prev, pager, next, sizes,jumper"
      :total="totals">
    </el-pagination>
      </el-card>
      <!-- 新增文章 -->
      <el-dialog @close="closeDrawer"
  :title="drawerTitle"
  :visible.sync="dialogVisible"
  width="50%"
  :before-close="handleClose">
  <div v-if="drawerType === 'preview'" class="article-preview">
            <h2>{{aaa.title}}</h2>
            <p>{{aaa.createTime|parseTimeByString}}</p>
            <div v-html="aaa.articleBody"></div>
          </div>
  <el-form v-else :model="form1" :rules="rules1" ref="form1" label-width="80px">
    <el-form-item label="文章标签" prop="title">
      <el-input v-model="form1.title" placeholder="请输入文章标签"></el-input>
    </el-form-item>
     <el-form-item label="文章内容" prop="articleBody">
         <quillEditor v-model="form1.articleBody" :options="editorOption"
              ref="myQuillEditor" @blur="$refs.form1.validateField('articleBody')"></quillEditor>
    </el-form-item>
     <el-form-item label="视频地址">
      <el-input v-model="form1.videoURL" placeholder="请输入视频地址"></el-input>
    </el-form-item>
      <el-form-item class="btn1"  >
      <el-button type="primary" @click="submit">确认</el-button>
      <el-button @click="close1">取消</el-button>
    </el-form-item>
  </el-form>
  <!-- 预览 -->
  <!-- <div>
    <h2>{{aaa.title}}</h2>
    <p>{{aaa.createTime|parseTimeByString}}</p>
    <div class="xiaolin" v-html="aaa.articleBody"></div>
  </div> -->
  <!-- <span slot="footer" class=
  "dialog-footer">
    <el-button >取 消</el-button>
    <el-button type="primary" >确 定</el-button>
  </span> -->
</el-dialog>
<!-- 视频 -->
<el-dialog :visible.sync="dialogVisible2" width="0" :show-close='false'>
<video :src="item.videoURL" class="videoplus" controls></video>
<i class="el-icon-close cha" @click="lwl"></i>
</el-dialog>
    </div>
    <!-- <div class="mask" v-if="videoState == true" @click="masksCloseFun">
      <video :src="form1.videoURL" controls="controls" autoplay="autoplay"></video>
    </div> -->
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { list, remove, changeState, add, update, detail } from '../../api/hmmm/articles'
export default {
  components: {
    quillEditor
  },
  data () {
    return {
      drawerType: 'add',
      dialogVisible: false,
      dialogVisible2: false,
      // 富文本配置项
      editorOption: {
        modules: {
          // theme: 'snow',
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote'], // 引用
            ['code-block', 'image', 'link'], // 代码块 上传图片 上传视频,
            ['video']
          ]
        }
      },
      rules1: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['blur', 'change'] }
        ],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: ['blur', 'change'] }]
      },
      // rules: {
      //   keyword: [{ required: true, message: '请输入文章标题', trigger: ['blur'] }]
      // },
      form: {
        keyword: '',
        value: ''

      },
      form1: {
        articleBody: '',
        title: '',
        videoURL: this.videoURL || ''

      },
      tableData: [],
      totals: 0,
      pagesize: 10,
      page: 1,
      item: {},
      aaa: []

    }
  },
  created () {
    this.initData()
  },
  computed: {
    drawerTitle () {
      let title = '标题'
      if (this.drawerType === 'add')title = '新增文章'
      if (this.drawerType === 'preview')title = '预览文章'
      if (this.drawerType === 'edit')title = '修改文章'
      return title
    }
  },
  methods: {
    close1 () {
      this.closeDrawer()
    },
    videoFn (url) {
      // this.form1.videoState = true
      // this.form1.videoURL = url
      this.item = url
      console.log(this.item)
      this.dialogVisible2 = true
    },
    lwl () {
      this.dialogVisible2 = false
    },
    async submit () {
      await this.$refs.form1.validate()
      if (this.drawerType === 'add') {
        await add(this.form1)
        this.$message.success('操作成功')
      }
      if (this.drawerType === 'edit') {
        await update(this.form1)
        this.$message.success('操作成功')
      }

      this.page = 1
      this.initData()
      this.closeDrawer()
      // console.log(123)
    },
    closeDrawer () {
      this.dialogVisible = false
      this.form1 = {
        articleBody: '',
        title: '',
        videoURL: ''
      }
      if (this.drawerType !== 'preview') {
        this.$refs.form1.resetFields()
      }
    },
    // 获取文章列表
    async initData () {
      // const res = await list({
      //   page: this.page,
      //   pagesize: this.pagesize
      // })
      // console.log(res)
      // this.tableData = res.data.items
      // this.totals = res.data.counts
      const obj = {
        page: this.page,
        pagesize: this.pagesize,
        // 关键字判断
        keyword: this.form.keyword
      }
      // 状态判断
      if (this.form.value !== '') {
        obj.state = this.form.value
      }
      const res = await list(obj)
      console.log(res)
      this.totals = res.data.counts
      this.tableData = res.data.items
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      // console.log(`每页 ${val} 条`)
      this.page = 1
      this.pagesize = val
      this.initData()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.initData()
    },
    clearFn () {
      this.form = {
        keyword: ''
      }
    },
    searchFn () {
      this.initData()
      console.log(this.from)
    },
    async del (id) {
      console.log(id)
      await this.$confirm('此操作将永远删除该文章，是否继续？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
      const res = await remove({ id })
      console.log(res)
      this.$message.success('删除成功')
      if (this.tableData.length === 1 && this.page > 1) {
        this.page--
      }
      this.initData()
    },
    async stateFn (row) {
      console.log(row.state)
      const state = row.state ? '0' : '1'
      await changeState({ state, id: row.id })
      this.$message.success('操作成功')
      this.initData()
      this.$forceUpdate()
    },
    async  openDrawer (type, id) {
      console.log(type, id)
      this.dialogVisible = true
      this.drawerType = type
      if (type !== 'add') {
        const res = await detail({ id })
        console.log(res)
        this.aaa = res.data
        // this.parseTime()
        this.form1 = {
          ...res.data
        }
      } else {
        this.form1 = {
          articleBody: '',
          title: '',
          videoURL: ''
        }
      }
    },
    handleClose () {
      this.dialogVisible = false
      console.log('关闭了')
    }
  }
}
</script>

<style scoped lang='scss'>
.form{
  display: flex;
  justify-content: space-between;
.left{
  display: flex;

}
 }
 .header{
  background-color: #f4f4f5;
  color: #909399;
  font-size: 13px;
  height: 39px;
  padding: 8px 16px;
  margin-bottom: 15px;
  line-height: 24px;
  border-radius: 4px;
 }
 .actions{
  display: flex;
  justify-content: space-around;
 }
 .pag{margin-bottom: 15px;
       margin-top: 15px;   }
.btn1{
  position: absolute;
  right: 15px;
  bottom: -10px;
}
.quill-editor {
 ::v-deep .ql-editor {
  height: 200px;
 }
}
.el-icon-film{
  color: blue;
}
.videoplus{
position: fixed;
top: 90px;
left: 250px;
width: 1000px;
height: 500px;

}
.cha{
  position: fixed;
  border-radius: 50% 50%;
  top: 30px;
  left: 730px;
  width: 50px;
  height: 50px;
  background-color: rgba(225,225,225,0.5);
  text-align: center;
  line-height: 50px;
  font-size: 30px;
}
.article-preview{
   padding: 0 40px 40px 40px;
  > div {
    background-color: #f5f5f5;
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    // padding-bottom: 30px;
    padding: 10px;
}}

</style>
